package com.yangquan.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author yangquan
 * @date 2025/4/6
 * @apiNote
 */
public class CallableDemo {

    public static void main(String[] args) throws Exception {

        // 创建一个带返回值的任务对象
        CallableExample callableExample = new CallableExample();

        // 通过Future类结合Callable类来创建带有返回值的线程
        FutureTask<String> futureTask = new FutureTask<>(callableExample);

        // 将futureTask传递给Thread对象
        Thread thread = new Thread(futureTask);
        thread.start();

        // 等待子线程执行结束
        thread.join();

        // 获取子线程执行结果
        String result = futureTask.get();
        System.out.println(result);
    }
}

class CallableExample implements Callable<String> {


    @Override
    public String call() throws Exception {
        return "子线程执行结果：" + Thread.currentThread().getName();
    }
}
